package com.cmas.platform.service.impl;

import java.util.List;


import com.cmas.platform.utils.ContentCheckUtil;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmas.platform.mapper.PlatformConfigMapper;
import com.cmas.platform.domain.PlatformConfig;
import com.cmas.platform.service.PlatformConfigService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 平台配置Service业务层处理
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-03-15
 */
@Service
public class PlatformConfigServiceImpl extends ServiceImpl<PlatformConfigMapper, PlatformConfig> implements PlatformConfigService {

    private static final Logger log = LoggerFactory.getLogger(PlatformConfigServiceImpl.class);



    /**
     * 查询平台配置列表
     * @param platformConfig 平台配置
     * @return 平台配置
     */
    @Override
    public List<PlatformConfig> selectPlatformConfigList(PlatformConfig platformConfig) {
        QueryWrapper<PlatformConfig> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(platformConfig.getKey()))
         {
           queryWrapper.lambda().eq(PlatformConfig::getKey,platformConfig.getKey());
         }
        if(StringUtils.isNotEmpty(platformConfig.getExplain()))
         {
           queryWrapper.lambda().like(PlatformConfig::getExplain,platformConfig.getExplain());
         }
        if(StringUtils.isNotEmpty(platformConfig.getStatus()))
        {
            queryWrapper.lambda().like(PlatformConfig::getStatus,platformConfig.getStatus());
        }
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 批量删除平台配置
     * @param ids 需要删除的平台配置ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deletePlatformConfigByIds(String[] ids) {
        return baseMapper.deletePlatformConfigByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    @Override
    public boolean checkCodeUnique(PlatformConfig platformConfig) {
        QueryWrapper<PlatformConfig> queryWrapper = new QueryWrapper<>();
        PlatformConfig notEqual = baseMapper.selectOne(queryWrapper
                .lambda().eq(PlatformConfig::getKey, platformConfig.getKey()));
        if (notEqual != null) {
            return !notEqual.getId().equals(platformConfig.getId());
        }
        return false;
    }

    @Override
    public int updatePlatformConfig(PlatformConfig platformConfig) {
        int update=0;
        if(platformConfig.getKey().equals("sensitive_word_whitelist")
                ||platformConfig.getKey().equals("sensitive_word_blacklist")){
            PlatformConfig oldPlatformConfig=baseMapper.selectOne(new QueryWrapper<PlatformConfig>().lambda()
                    .eq(PlatformConfig::getKey,platformConfig.getKey()));
            update = baseMapper.updateById(platformConfig);
            if (!oldPlatformConfig.getValueString().equals(platformConfig.getValueString())){
                ContentCheckUtil.refresh();
            }
        }else {
            update = baseMapper.updateById(platformConfig);
        }
        return update;
    }

}
